"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var tag_1 = require("./tag");
var coupons_service_1 = require("../../lib/service/admin/coupons.service");
var banner_service_1 = require("./banner.service");
var api_1 = require("../../../config/api");
var BannerComponent = /** @class */ (function () {
    function BannerComponent(router, couponsService, bannerService, adOssUploadService, _message) {
        this.router = router;
        this.couponsService = couponsService;
        this.bannerService = bannerService;
        this.adOssUploadService = adOssUploadService;
        this._message = _message;
        this.currentPage = 1;
        this.itemsPerPage = 20;
        this.isShow = false;
        this.addActivity = new tag_1.AddActivity();
        this.editActivity = new tag_1.EditActivity();
        this.fileList = [];
        this.picList = [];
        this.fileList1 = [];
        this.picList1 = [];
        this.taskList = [];
        this.task_ids = [];
        this.checkedTaskInfo = [];
        this.taskModalVisible = false;
        this.taskPage = 1;
        this.taskPageSize = 5;
        this.taskTotal = 0;
        this.isConfirmTaskLoading = false;
        this.taskLoading = false;
        this.activityList = [];
        this.submitStatus = false;
        this.confirmStatus = null;
        this.confirmId = null;
        this.confirmTitle = '';
        this._isSpinning = false;
        this.picError = false;
        this.picError1 = false;
        this.name = '';
        this.model = '';
        this.orderField = '';
        this.orderSort = '';
    }
    BannerComponent.prototype.ngOnInit = function () {
        this.getActivityList(1);
    };
    BannerComponent.prototype.fileChange = function () {
        if (this.fileList.length == 0 && this.picList.length == 0) {
            this.picError = true;
        }
        else {
            this.picError = false;
        }
    };
    BannerComponent.prototype.fileChange1 = function () {
        if (this.fileList1.length == 0 && this.picList1.length == 0) {
            this.picError1 = true;
        }
        else {
            this.picError1 = false;
        }
    };
    BannerComponent.prototype.showAddModal = function () {
        this.addModal.show();
    };
    BannerComponent.prototype.showEditModal = function () {
        this.editModal.show();
    };
    BannerComponent.prototype.getActivityList = function (page) {
        var _this = this;
        this._isSpinning = true;
        this.bannerService.getActivityList(page, this.itemsPerPage).subscribe(function (res) {
            if (res.code == 200) {
                _this._isSpinning = false;
                _this.activityList = res.data.data;
                _this.totalItems = res.data.total;
                _this.activityList.map(function (e, i) {
                    if (e.pic) {
                        e.pic = api_1.AliyunOssOutputUrl + "/" + e.pic + "?update=" + new Date().getTime();
                    }
                });
            }
        });
    };
    BannerComponent.prototype.showConfirmModal = function (id, title, status) {
        this.confirmModal.show();
        this.confirmStatus = status;
        this.confirmId = id;
        this.confirmTitle = title;
    };
    BannerComponent.prototype.closeConfirmModal = function () {
        this.confirmModal.hide();
    };
    BannerComponent.prototype.changeStatus = function () {
        var _this = this;
        this.bannerService.changeStatus({ 'status': this.confirmStatus, 'id': this.confirmId }).subscribe(function (res) {
            if (res.code === 200) {
                _this.confirmModal.hide();
                _this.type = 'success';
                _this.message = '修改状态成功!';
                _this.isShow = true;
                setTimeout(function () {
                    _this.getActivityList(_this.currentPage);
                }, 300);
            }
        });
    };
    BannerComponent.prototype.closeTaskModal = function () {
        this.taskModalVisible = false;
    };
    BannerComponent.prototype.goActivity = function () {
        this.router.navigate(['tuike/activity/add']);
    };
    BannerComponent.prototype.dealTask = function (e, data) {
        var _this = this;
        this.taskList.map(function (item, i) {
            if (item.id == data.id) {
                if (_this.task_ids.length == 0) {
                    if (e) {
                        item['checked'] = true;
                        _this.task_ids.push(data);
                    }
                }
                else {
                    if (e) {
                        var num_1 = 0;
                        _this.task_ids.map(function (item1, i) {
                            if (item1.id != data.id && num_1 === 0) {
                                item['checked'] = true;
                                _this.task_ids.push(data);
                                num_1++;
                            }
                            return;
                        });
                    }
                    else {
                        _this.task_ids.map(function (item2, i) {
                            if (item2.id == data.id) {
                                item['checked'] = false;
                                _this.task_ids.splice(i, 1);
                                return;
                            }
                            return;
                        });
                    }
                }
            }
        });
        this.checkedTaskInfo = this.task_ids;
    };
    BannerComponent.prototype.delete = function () {
        var _this = this;
        this.bannerService.deleteActivity({ id: this.confirmId }).subscribe(function (res) {
            if (res.code == 200) {
                _this.confirmModal.hide();
                _this.type = 'success';
                _this.message = '删除成功';
                _this.isShow = true;
                setTimeout(function () {
                    _this.getActivityList(1);
                }, 300);
            }
        });
    };
    BannerComponent.prototype.uploadImg = function (activity_id) {
        var _this = this;
        this.adOssUploadService
            .getSignature('/v1/admin/tuike_activity/' + activity_id + '?callback=true')
            .subscribe(function (res1) {
            var length = _this.fileList.filter(function (item) {
                return item && item != "pic";
            }).length;
            var successLen = 0;
            if (length == 0) {
                // this.submitStatus = false;
                _this._message.create("success", '添加成功');
                return false;
            }
            _this.fileList.every(function (val, idx, array) {
                if (val && val != "pic") {
                    _this.adOssUploadService.fileUpload(val, res1.data, 'tuike_activity_pic' + '.' + val.name.replace(/.+\./, '').toLowerCase(), 'image')
                        .subscribe(function (res2) {
                        successLen++;
                        if (successLen == length) {
                            _this.submitStatus = false;
                            _this._message.create('success', '添加成功');
                            _this.addModal.hide();
                            _this.getActivityList(1);
                        }
                    });
                }
                return true;
            });
        });
    };
    BannerComponent.prototype.addActivityFormSubmit = function (e) {
        var _this = this;
        e.stopPropagation();
        if (this.checkedTaskInfo.length == 0) {
            this.isShow = false;
            this.message = '请添加任务';
            this.type = 'error';
            return;
        }
        var data = Object.assign(this.addActivity);
        data['task_ids'] = [];
        this.checkedTaskInfo.map(function (e, i) {
            data['task_ids'].push(e.id);
        });
        this.submitStatus = true;
        this.bannerService.addActivity(data).subscribe(function (res) {
            if (res.code == 200) {
                var id = res.data.id;
                _this.uploadImg(id);
            }
        });
    };
    BannerComponent.prototype.goEdit = function (id) {
        this.router.navigate(['tuike/activity/add'], {
            queryParams: {
                id: id
            }
        });
    };
    BannerComponent.prototype.editActivityFormSubmit = function (e) {
        e.stopPropagation();
        if (this.checkedTaskInfo.length == 0) {
            this.isShow = false;
            this.message = '请添加任务';
            this.type = 'error';
            return;
        }
        var data = Object.assign(this.addActivity);
        data['task_ids'] = [];
        this.checkedTaskInfo.map(function (e, i) {
            data['task_ids'].push(e.id);
        });
    };
    BannerComponent.prototype.pageChanged = function (event) {
        window.scrollTo(0, 0);
        this.currentPage = event.page;
        this.getActivityList(this.currentPage);
    };
    BannerComponent.prototype.promptIsShow = function (event) {
        this.isShow = event;
    };
    __decorate([
        core_1.ViewChild('addModal')
    ], BannerComponent.prototype, "addModal", void 0);
    __decorate([
        core_1.ViewChild('editModal')
    ], BannerComponent.prototype, "editModal", void 0);
    __decorate([
        core_1.ViewChild('confirmModal')
    ], BannerComponent.prototype, "confirmModal", void 0);
    BannerComponent = __decorate([
        core_1.Component({
            selector: 'app-banner',
            templateUrl: './banner.component.html',
            styleUrls: ['./banner.component.scss'],
            providers: [coupons_service_1.CouponsService, banner_service_1.BannerService]
        })
    ], BannerComponent);
    return BannerComponent;
}());
exports.BannerComponent = BannerComponent;
